package com.wjf.accesscontrol.ui.read

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.wjf.accesscontrol.base.MVVMBaseFragment
import com.wjf.accesscontrol.databinding.FragmentIdCardBinding
import com.wjf.accesscontrol.entity.IdCardEntity
import com.wjf.accesscontrol.entity.SocialCardEntity
import com.wjf.accesscontrol.entity.StationDetailEntity
import com.wjf.accesscontrol.net.utils.Status
import com.wjf.accesscontrol.ui.read.dialog.DIALOG_TYPE_1
import com.wjf.accesscontrol.ui.read.dialog.ResultDialogCallback
import com.wjf.accesscontrol.util.AppUtils
import com.wjf.accesscontrol.util.Constant
import com.wjf.accesscontrol.util.CoroutineUtils
import com.wjf.accesscontrol.util.LogUtils
import com.wjf.accesscontrol.util.READ_ERR_CARD_NULL
import com.wjf.accesscontrol.util.READ_ERR_NO_CONNECT
import com.wjf.accesscontrol.util.READ_ERR_PSAM_NULL
import com.wjf.accesscontrol.util.sdkUtils.ReadCallback
import com.wjf.accesscontrol.util.sdkUtils.ReadCardUtils
import com.wjf.accesscontrol.util.TYPE_1
import com.wjf.accesscontrol.util.ToastUtils
import com.wjf.accesscontrol.util.sdkUtils.TelpoUtils
import kotlinx.coroutines.delay

/**
 * @Description
 * @Author WuJianFeng
 * @Date 2024/1/29 16:30
 *
 */

class IdCardFragment : MVVMBaseFragment<ReadViewModel, FragmentIdCardBinding>(), ReadCallback, ResultDialogCallback {

    override fun initViewModel() = ViewModelProviders.of(this).get(ReadViewModel::class.java)

    override fun initViewBinding(inflater: LayoutInflater, container: ViewGroup?) =
        FragmentIdCardBinding.inflate(inflater,container,false)

    override fun initData() {
        ReadCardUtils.getInstance().setReadCallback(this)
        vm.countDownTimer()

        vm.timeValue.observe(this, Observer {
            binding.tvCount.text = "${it}s"
        })

        vm.reRead.observe(this, Observer {
            if (it){
                vm.reRead.postValue(false)
                startRead()
            }
        })

        vm.timerFinish.observe(this, Observer {
            if (it){
                activity?.finish()
            }
        })
    }

    private fun startRead() {
        ReadCardUtils.getInstance().readIdCard()
    }

    override fun onResume() {
        super.onResume()
        startRead()
    }

    override fun idData(idCardEntity: IdCardEntity) {
        LogUtils.dw("__read-id","$idCardEntity")
        TelpoUtils.setRelayPowerOn()

        AppUtils.showResultDialog(
            fragmentManager = childFragmentManager,
            resultDialogCallback = this,
            type = DIALOG_TYPE_1,
        )

        vm.stationDetail().observe(this, Observer {
            it?.let { resource ->
                if (resource.status == Status.SUCCESS) {
                    if (resource.data is StationDetailEntity){
                        Constant.province = "${resource.data.province}000000"
                        Constant.city = "${resource.data.city}000000"
                        Constant.region = "${resource.data.region}000000"
                        Constant.stationId = "${resource.data.id}"
                        Constant.stationName = "${resource.data.name}"
                        upload(idCardEntity)
                    }
                }
            }
        })
    }

    fun upload(idCardEntity: IdCardEntity){
        vm.upload(
            idNum = idCardEntity.idNumber,
            name = idCardEntity.name,
            sex = idCardEntity.gender,
            accessType = TYPE_1
        ).observe(this, Observer {
            when(it.status){
                Status.SUCCESS -> {
                }
                Status.ERROR -> {
                }
            }
        })
    }

    override fun socialData(socialCardEntity: SocialCardEntity) {}

    override fun errMsg(msg: String?) {
        vm.job?.cancel()
        when(msg){

            READ_ERR_CARD_NULL -> {
                vm.job = CoroutineUtils.instance.launch {
                    delay(1000)
                    vm.reRead.postValue(true)
                }
            }
            READ_ERR_PSAM_NULL,
            READ_ERR_NO_CONNECT -> {
                ToastUtils.show(msg)
                activity?.finish()
            }

            else -> {
                vm.job = CoroutineUtils.instance.launch {
                    delay(1000)
                    vm.reRead.postValue(true)
                }
            }

        }
    }



    override fun dismiss() {
        activity?.finish()
    }

    override fun onDestroy() {
        super.onDestroy()
        vm.release()
    }
}